<?php
namespace Home\Controller;
use Think\Controller;
class ProductController extends Controller {
    public function detail($id){
    	$product=M('product')->where('id='.$id)->find();
        $imgs=M('product_img')->where('product_id='.$id)->limit(4)->select();
        $product=$this->assign('product',$product);
        $imgs=$this->assign('imgs',$imgs);
		$this->display();
    }
    public function lists($catalog_id){
        $brand_id=I('get.brand');
        $current_catalog=M('product_catalog')->where('id='.$catalog_id)->find();//获取当前分类内容
        $parent_id=$current_catalog['parent_id'];
        $parent_catalog=M('product_catalog')->where('id='.$parent_id)->find();//获取当前分类的父类内容
        $brother_catalogs=M('product_catalog')->where('parent_id='.$parent_id)->select();//获取当前分类的兄弟分类
        $child_catalogs=M('product_catalog')->where('parent_id='.$current_catalog['id'])->select();//获取当前分类的子分类
         if($current_catalog['parent_id']==0){
            $child_catalogs=M('product_catalog')->where('parent_id='.$current_catalog['id'])->select();
            $this->redirect('Home/Product/lists/catalog_id/'.$child_catalogs[0]['id']);
        }
        $catalogs=array($current_catalog['id']);
        foreach ($child_catalogs as $c) {
            $catalogs[]=$c['id'];
        }
        $where='catalog_id in('.implode(',',$catalogs).')';
        if($brand_id){
            $where.='and brand_id='.$brand_id;
        }

        $products_imgs=M('product')->where($where)->select();
        $product=M('product')->where('catalog_id in('.implode(',',$catalogs).')')->select();
        $brand_ids=array();
        foreach ($product as $p) {
           $brand_ids[]=$p['brand_id'];
        }
        $brand_ids=array_unique($brand_ids);
        $brands=M('product_brand')->where('id in('.implode(',',$brand_ids).')')->select();
        $this->assign('parent_catalog',$parent_catalog);
        $this->assign('child_catalogs',$brother_catalogs);
        $this->assign('catalog',$current_catalog);
        $this->assign('products_imgs',$products_imgs);
         $this->assign('brands',$brands);
        $this->display();
    }
}